package com.sinai.mshab.data.gwtwidgets;

import com.google.gwt.user.client.ui.ListBox;
import com.sinai.mshab.data.DataManager;

/**
 * A concrete implementation of {@link DataManager} for GWT {@link ListBox}
 * widgets. Current this only supports {@code ListBox}es that aren't
 * multiselected. If the {@code ListBox} is multiple select an
 * {@link IllegalStateException} is thrown.
 * 
 * @author Arthur Kalmenson
 */
public class ListBoxDataManager implements DataManager<ListBox, String> {

	/**
	 * The default value that the {@link ListBox}es being processed.
	 */
	private String defaultValue;

	/**
	 * Returns the currently selected value. If the value is equal to
	 * {@code defaultValue} or nothing is selected, null will be returned. If
	 * the given {@link ListBox} has multiple select enabled, we'll throw an
	 * {@link IllegalStateException}.
	 * 
	 * @param widget
	 *            the {@code ListBox} to get the value from.
	 * @return the currently selected value.
	 * @throws IllegalStateException
	 *             if the given {@link ListBox} has multiple select enabled.
	 */
	public String getData(ListBox widget) {

		// we don't support multiple select.
		if (widget.isMultipleSelect()) {
			throw new IllegalStateException("Multiple select ListBoxes "
					+ "are currently not supported");
		}

		String result = null;

		// if an index is selected.
		if (widget.getSelectedIndex() > -1) {

			// and either the default value is null or it's not equal to the
			// selected index, we save the result.
			if (defaultValue == null
					|| !widget.getValue(widget.getSelectedIndex()).equals(
							defaultValue)) {
				result = widget.getValue(widget.getSelectedIndex());
			}
		}

		return result;
	}

	/**
	 * Sets the selected value of {@code widget}. If the given {@link ListBox}
	 * has multiple select enabled, we'll throw an {@link IllegalStateException}.
	 * 
	 * @param widget
	 *            the {@code ListBox} to get the value from.
	 * @param data
	 *            the new value for {@code widget}.
	 * @throws IllegalStateException
	 *             if the given {@link ListBox} has multiple select enabled.
	 */
	public void setData(ListBox widget, String data) {

		// we don't support multiple select.
		if (widget.isMultipleSelect()) {
			throw new IllegalStateException("Multiple select ListBoxes "
					+ "are currently not supported");
		}

		// go through the list of
		for (int i = 0; i < widget.getItemCount(); i++) {
			if (widget.getValue(i).equals(data)) {
				widget.setSelectedIndex(i);
				break;
			}
		}
	}

	/**
	 * Returns the default value that will be skipped if encountered.
	 * 
	 * @return the default value that will be skipped if encountered.
	 */
	public String getDefaultValue() {
		return defaultValue;
	}

	/**
	 * Sets the default value that will be skipped if encountered. An example of
	 * a default value would be "Choose X" which we wouldn't want returned.
	 * 
	 * @param defaultValue
	 *            the default value that will be skipped if encountered.
	 */
	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

}
